package com.exam.serviceimpl;

import com.exam.entity.Admin;
import com.exam.entity.ApiResult;
import com.exam.mapper.AdminMapper;
import com.exam.service.AdminService;
import com.exam.util.ApiResultHandler;
import com.exam.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;

    private  ApiResult  apiResult= new ApiResult() ;


    /**
     * 查询所有管理员的数据
     * @return
     */
    @Override
    public ApiResult findAll() {
        List<Admin> all = adminMapper.findAll();
        if (all == null) {
            return ApiResultHandler.buildApiResult(500,"没有查询到数据","");
        }
        return ApiResultHandler.success(all);
    }


    /***
     * 根据管理员的id查询对应的管理员的信息
     * @param adminId 管理员的id
     * @return
     */
    @Override
    public ApiResult findById(Integer adminId) {
        if (adminId == null) {
            return ApiResultHandler.buildApiResult(500,"参数传递为空","");
        }
        Admin admin = adminMapper.findById(adminId);
        return ApiResultHandler.success(admin);
    }

    /**
     * 根据管理员id删除管理员信息
     * @param adminId 管理员的id
     * @return
     */
    @Override
    public ApiResult deleteById(Integer adminId) {
        if (adminId == null) {
            return ApiResultHandler.buildApiResult(500,"参数传递为空","");
        }
        int i = adminMapper.deleteById(adminId);
        if (i<0){
            return ApiResultHandler.buildApiResult(500,"删除失败","");
        }
        return ApiResultHandler.success(i);
    }

    /**
     * 根据ID和需要修改的数据进行修改
     * @param admin
     * @return
     */
    @Override
    public ApiResult update(Admin admin) {
        if (admin==null){
            return ApiResultHandler.buildApiResult(500,"参数传递为空","");
        }
        int update = adminMapper.update(admin);
        if (update<0){
            return ApiResultHandler.buildApiResult(500,"删除失败","");
        }
        return ApiResultHandler.success(update);
    }

    /**
     * 添加管理员信息
     * @param admin
     * @return
     */
    @Override
    public ApiResult add(Admin admin) {
        if (admin==null){
            return ApiResultHandler.buildApiResult(500,"参数传递为空","");
        }
        //使用MD5算法对密码进行加密处理
        //String md5PW = MD5Utils.getMD5(admin.getPwd());
        //admin.setPwd(md5PW);

        int add = adminMapper.add(admin);
        if (add<0){
            return ApiResultHandler.buildApiResult(500,"删除失败","");
        }
        return ApiResultHandler.success(add);
    }
}
